/*
 * Compression.cpp
 *
 *  Copyright   : Copyright © 2011 Binda&Aeolus; All Rights Reserved
 *  Created on: 2011-3-20
 *  Author      :洪春晓
 */
#include<iostream>
#include<fstream>
#include "Compression.h"

using namespace std;

Compression::Compression(int _pixel_num,int *_pixel_sequence,int *_store_size,int *_length_sequ,int *_pixel_len)
{
	pixel_num = _pixel_num;
	pixel_sequence = _pixel_sequence;
	store_size = _store_size;
	length_sequ = _length_sequ;
	pixel_len = _pixel_len;
}
Compression::Compression()
{

}
void Compression::uncompress_file(const string source_filename,int skit_num)//,const string uncompress_filename)
{
	read_from_file(source_filename,skit_num);
	cout<<"像素总数为："<<pixel_num<<endl;
}
void Compression::compress_file(const string filename)
{
	int bMax = 0;
	store_size[0] = 0;
	for(int i=1; i <= pixel_num; i++)
	{
		pixel_len[i] = length(pixel_sequence[i]);
		bMax = pixel_len[i];
		store_size[i] = store_size[i-1] + 1*bMax;
		length_sequ[i] = 1;
		for(int j=2; j<=i && j<=LMAX; j++)
		{
			if(pixel_len[i-j+1] > bMax)
				bMax = pixel_len[i-j+1];
			if(store_size[i] > store_size[i-j] + j * bMax)
			{
				store_size[i] = store_size[i-j] + j * bMax;
				length_sequ[i] = j;
			}
		}
		store_size[i] += HEADER;
	}
	output(filename);
}
void Compression::output(const string filename)
{
	//cout<<"最佳值为："<<store_size[pixel_num]<<endl;
	int seg_index = 0;
	traceback(pixel_num,seg_index);
	store_size[seg_index] = pixel_num;//第seg_index段（最后一段）在pixel_num结束

	cout <<",共分为的段数为："<<seg_index<<endl;
	for(int j=1; j <= seg_index; j++)
	{
		length_sequ[j] = length_sequ[store_size[j]];
		pixel_len[j] = pixel_len[store_size[j]];
	}

	write_to_file(filename,seg_index);
}
void Compression::traceback(int current_pixedl_num,int &segmentIndex)
{
	if(current_pixedl_num == 0)
		return;
	traceback(current_pixedl_num - length_sequ[current_pixedl_num],segmentIndex);
	store_size[segmentIndex] = current_pixedl_num - length_sequ[current_pixedl_num];//保存第i段在开始公交车，即第i-1段在结束位置
	segmentIndex ++ ;
}

int Compression::length(int n)
{
		int k=1;
		n = n/2;
		while(n>0)
		{
			k++;
			n/=2;
		}
		return k;
}
void Compression::write_to_file(const string filename,int seg_num)
{
	//格式
	//第一行 段数
	//其他行  像素值    个数  位数
	//
	ofstream output;
	output.open(filename.c_str());//,ios::app);

	output<<seg_num<<endl;

	for(int i=1; i<=seg_num; i++)
	{
		output<<pixel_sequence[i]<<" "<<length_sequ[i]<<" "<<pixel_len[i]<<" ";
	}
	output.close();
}
int Compression::read_from_file(const string filename,int skit_num)
{
	int seg_num;
	ifstream input(filename.c_str());
	input.ignore(skit_num);

	input>>seg_num;

	pixel_sequence = new int[seg_num+1];
	length_sequ = new int[seg_num+1];
	pixel_len = new int[seg_num+1];
	for(int i=1; i<=seg_num; i++)
	{
		input>>pixel_sequence[i]>>length_sequ[i]>>pixel_len[i];
	}
	input.close();

	//测试
	pixel_num = 0;
	for(int i=1; i<=seg_num; i++)
	{
		//cout<<pixel_sequence[i]<<"  "<<length_sequ[i]<<"  "<<pixel_len[i]<<endl;
		pixel_num += length_sequ[i];
	}
	return 0;
}
Compression::~Compression() {
	// TODO Auto-generated destructor stub
}
